<?php
/**
 * Created by zhangyh.
 * User: zhangyh
 * Date: 2018/11/5
 * Time: 17:00
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\BaseController;
use App\Service\FollowService;
use Illuminate\Http\Request;

class MemberController extends BaseController
{
    /**
     * 关注的好友列表
     * @param Request $request
     * @author 许立 2018年11月6日
     */
    public function followList(Request $request, FollowService $followService)
    {
        // 获取关注列表
        $result = $followService->getAllList(['follower_id' => $request->input('mid'), 'status' => FollowService::STATUS_FOLLOW]);
        $result['data'] = $followService->handleMemberInfo($result['data']);

        // 用户信息
        success($result);
    }

    /**
     * 关注和取消关注
     * @param Request $request
     * @author 许立 2018年11月6日
     */
    public function follow(Request $request, FollowService $followService)
    {
        // 获取关注列表
        $result = $followService->getAllList(['follower_id' => $request->input('mid'), 'status' => FollowService::STATUS_FOLLOW]);
        $result['data'] = $followService->handleMemberInfo($result['data']);

        // 用户信息
        success($result);
    }


    /**
     * 添加奖品
     * @throws WebException
     * @author: 梅杰 20101106
     */
    public function addAddress(Request $request)
    {
        $input = $request->only(['member_id','consignee_name','phone','province','city','area','detail','postcodes']);
        //数据验证
        $messages = [
            'member_id.required'        => '请填写赞助商名称!',
            'consignee_name.required'   => '请填写赞助商描述!',
            'phone.required'            => '请填写赞助商名称!',
            'province.required'         => '请填写赞助商描述!',
            'city.required'             => '请填写赞助商名称!',
            'area.required'             => '请填写赞助商描述!',
            'detail.required'           => '请填写赞助商名称!',
            'postcodes.required'        => '请填写赞助商描述!',
        ];
        $validator = Validator::make($input,[
            'name' => 'required' ,
            'desc' => 'required' ,
        ],$messages);

        if ($validator->fails()) {
            throw new WebException($validator->errors());
        }
        if ((new AddressService())->add($input)) {
            success();
        }
        throw new WebException('添加失败');
    }
}